#/*************************************************************************
#    > File Name: examples/example_ARCH.py
#    > Author: Yan Wang
#    > Mail: wangyan@imnu.edu.cn
#    > Created Time: 2022年06月29日 星期三 23时28分59秒
# ************************************************************************/
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
sys.path.append('../Lib/')
from Load_Lib import * 


print("The examples in this file are:")   
ex_name={"1":"Cal_Parton_Cut_Xection"
,"2":"Replace_Para_in_ARCH"
,"3":"Generate_Multi_ARCH_Control_File"
,"4":"Find_ARCH_xection"
,"5":"Replace_Para_in_ARCH"
,"6":"Generate_Multi_ARCH_Control_File"
,"7":"Get_Cross_after_ARCH"
,"8":"Collect_ARCH_Cross"
,"all":"all examples"}    #字典，所有对应函数为Lib/ARCH_Operation.py
for key in ex_name:
	print(key, ex_name[key])       #if条件语句，如果key在ex_name里面，则输出eg：1 Cal_Parton_Cut_Xection
num=input("请输入想要运行的例子：")


if num == "1" or num == ex_name["1"] or num == "all":
# Cal_Parton_Cut_Xection
# 三个参数(A,B,C),A为输入文件(加了cut的数据),B为输入文件(未加cut的数据),C为加cut的数据文件
# 按照比例计算出加了cut的截面大小(内置比例算法)
# 最后输出为cut_cross列表
	cut_file_name=example_dir+"test_input/scan_run_[01-90]_cut.txt"
	nocut_file_name=example_dir+"test_input/scan_run_[01-90]_no_cut.txt"
	theoretical_parafile_name=example_dir+"test_input/data_grid_mh_mch.tsv"
	cut_cross=Exhibit_Function(Cal_Parton_Cut_Xection,[cut_file_name, nocut_file_name, theoretical_parafile_name])


# param setting


if num == "2" or num == ex_name["2"] or num == "all":
# Replace_Para_in_ARCH
# 两个参数(A,B),A为需要替换的路径文件,B替换值(替换标识符为$)
# cut_cross依次替换到input_para并且转换为列表
# 最后输出为替换过的列表
# {'path': [['PROCESS          : sigh_w4b_1', "RECORD_FILE      : 'tmp/sigh_w4b_1.dat'", "INPUT_FILE       : 'file_1.dat'"]
#   'file': [['FILE            : ["./examples/test_input/input_events/tag_1_delphes_events_1.root"]', 'CROSS_SECTION   : 19.53']]}
# 同样地，另外还有两组输出内容
	origin_path_list={"process":"PROCESS          : sigh_w4b_$fnum",
						"tmp" :"RECORD_FILE      : 'tmp/sigh_w4b_$fnum.dat'",
						"file":"INPUT_FILE       : 'file_$fnum.dat'"}
	origin_file_list={"file":"FILE            : [\"./examples/test_input/input_events/tag_1_delphes_events_$fnum.root\"]",
						"cross":"CROSS_SECTION   : $fvalue"}
	input_para={"path":origin_path_list, 
				"file":origin_file_list}
	cut_cross=[19.53, 66.25, 17.97]
	results=Exhibit_Function(Replace_Para_in_ARCH,[input_para, cut_cross])


if num == "3" or num == ex_name["3"] or num == "all":
# Generate_Multi_ARCH_Control_File 
# 三个参数(A,B,C),A为被替换的数值,B为被替换的文件,C为替换后的文件
# 输出结果与以上类似：
# 'path': [['PROCESS          : sigh_w4b_1',
#   "RECORD_FILE      : 'tmp/sigh_w4b_1.dat'",
#   "INPUT_FILE       : 'file_1.dat'"],
#    以及另外两组----sigh_w4b_2    sigh_w4b_3
# 'file': [['FILE            : ["/home/yanma/Desktop/test_input/input_events/tag_1_delphes_events_1.root"]',
#   'CROSS_SECTION   : 19.53'],
#   以及另外两组：'CROSS_SECTION   : 66.25  、  17.97
	cut_cross=[19.53, 66.25, 17.97]

	source = example_dir+'test_input/control_ARCH'
	target = example_dir+'test_output/control_ARCH_test'
	dir_name={"source":source,"target":target}


	para_path={"origin": {"process":"PROCESS          : sigh_w4b_$fnum",
						"tmp" :"RECORD_FILE      : 'tmp/sigh_w4b_$fnum.dat'",
						"file":"INPUT_FILE       : 'file_$fnum.dat'"},
				"source":"path.dat",
				"labels":{"PROCESS          : sigh_w4b_1":0,
						"RECORD_FILE      : 'tmp/sigh_w4b_1.dat'":0,
						"INPUT_FILE       : 'file_sigh_w4b_1.dat'":0}
				}
	file_folder_path=example_dir+"test_input/input_events/"
	origin_file="FILE            : [\""+file_folder_path+"tag_1_delphes_events_$fnum.root\"]"
	#para_file={"origin": {"file":"FILE            : [\""+file_folder_path+"tag_1_delphes_events_$fnum.root\"]",
	para_file={"origin": {"file":origin_file,
						"cross":"CROSS_SECTION   : $fvalue"},
				"source":"file.dat",
				"labels":{"FILE            : ['sig_Wh_4b_1_1.root']":0,
						"CROSS_SECTION   : 32.0566":0}
				}
	input_para={"path":para_path, 
				"file":para_file}
	Exhibit_Function(Generate_Multi_ARCH_Control_File,[cut_cross, dir_name,input_para])


if num == "4" or num == ex_name["4"] or num == "all":
# Find_ARCH_xection
# 三个参数(A,B,C),A为最后输出的文件名称,B为依次命名的文件个数,C为最后所有的文件目录
# 将total_file_num分别传参到ARCH_out_name中，最后全放在ARCH_summary_folder中

	ARCH_out_name=example_dir+"test_input/ARCH_output/sigh_w4b_{}.dat"
	total_file_num=5
	ARCH_summary_folder=example_dir+"test_output/ARCH_xection_Summary/"
	Exhibit_Function(Find_ARCH_xection, [ARCH_out_name,total_file_num, ARCH_summary_folder])



if num == "5" or num == ex_name["5"] or num == "all":
	cut_cross=[19.53, 66.25, 17.97]

	source = example_dir+'test_input/control_ARCH'
	target = example_dir+'test_output/control_ARCH_test'
	dir_name={"source":source,"target":target}


	para_path={"origin": {"process":"PROCESS          : sigh_w4b_$fnum",
						"tmp" :"RECORD_FILE      : 'tmp/sigh_w4b_$fnum.dat'",
						"file":"INPUT_FILE       : 'file_$fnum.dat'"},
				"source":"path.dat",
				"labels":{"PROCESS          : sigh_w4b_1":0,
						"RECORD_FILE      : 'tmp/sigh_w4b_1.dat'":0,
						"INPUT_FILE       : 'file_sigh_w4b_1.dat'":0}
				}
	f=open("/home/yancy/Software/ARCH_test/Auto_Running/auto_mab/Lib/examples/test_output/MG_folder/MG_Move/Events/THDM_CH_4b0j_90/THDM_CH_4b0j_90_moved_event_files.dat","r")
	lines=f.readlines()
	file_folder_path=""
	file_des=""
	for line_num, line in enumerate(lines):
		if line_num ==0:
			file_folder_path+=line
			file_des+="\"signal_1\"\n"
		else:
			file_folder_path+="                ,"+line
			file_des        +="                ,\"signal_1\"\n"

	origin_file=    "FILE            : ["+file_folder_path+"]"
	origin_file_des="FILE_DESCRIP    : ["+file_des+"]"
	#para_file={"origin": {"file":"FILE            : [\""+file_folder_path+"tag_1_delphes_events_$fnum.root\"]",
	para_file={"origin": {"fnum":"FILE_NUM        : {}".format(len(lines)),
							"cnum":"CHANNEL_NUM     : {}".format(len(lines)),
							"file":origin_file,
							"file_des":origin_file_des,
							"cross": "CROSS_SECTION   : $fvalue"},
				"source":"file.dat",
				"labels":{	"FILE_NUM        : 1":0,
							"CHANNEL_NUM     : 1":0,
							"FILE            : ['sig_Wh_4b_1_1.root']":0,
							"FILE_DESCRIP    : ['signal_1']":0,
							"CROSS_SECTION   : 32.0566":0}
				}
	input_para={"path":para_path, 
				"file":para_file}
	#Exhibit_Function(Generate_Multi_ARCH_Control_File,[cut_cross, dir_name,input_para])
	Generate_Multi_ARCH_Control_File(cut_cross, dir_name,input_para)

if num == "6" or num == ex_name["6"] or num == "all":
	print("test")
